<?php

/**
 * This is the model class for table "tender".
 *
 * The followings are the available columns in table 'tender':
 * @property string $tid
 * @property integer $pid
 * @property string $tendername
 * @property string $rid
 * @property integer $tendererid
 * @property integer $mode
 * @property string $saletime
 * @property string $opentime
 * @property integer $userid
 * @property integer $department
 * @property integer $status
 */
class Tender extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Tender the static model class
	 */

	public $search_project;
	public $search_user;
	public $search_tenderer;

	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tender';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('tid, pid, tendername, tendererid, mode, status', 'required'),
			array('tendererid, mode, userid, department, status', 'numerical', 'integerOnly'=>true),
			array('tid, rid', 'length', 'max'=>9),
			array('tendername', 'length', 'max'=>256),
			array('saletime, opentime, endtime', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('tid, pid, tendername, rid, search_tenderer,search_user,search_project, mode, saletime, opentime, userid, department, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'project'=>array(self::BELONGS_TO,'Project','pid'),
			'user'=>array(self::BELONGS_TO,'User','userid'),
			'tenderer'=>array(self::BELONGS_TO,'Tenderer','tendererid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'tid' => Yii::t('tender','Tid'),
			'pid' => Yii::t('tender','Pid'),
			'tendername' => Yii::t('tender','Tendername'),
			'rid' => Yii::t('tender','Rid'),
			'tendererid' => Yii::t('tender','Tenderer'),
			'mode' => Yii::t('tender','Tendermode'),
			'saletime' => Yii::t('tender','Saletime'),
			'endtime' => Yii::t('tender','Endtime'),
			'opentime' => Yii::t('tender','Opentime'),
			'userid' => Yii::t('tender','Userid'),
			'department' => Yii::t('tender','Department'),
			'status' => Yii::t('tender','Status'),
			'location' => Yii::t('tender','Location'),
			'price' => Yii::t('tender','Price'),
			'deposit' => Yii::t('tender','Deposit'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->with = array('project','user','tenderer');

		$criteria->compare('tid',$this->tid,true);
		$criteria->compare('project.projectname',$this->search_project,true);
		$criteria->compare('user.username',$this->search_user,true);
		$criteria->compare('tenderer.name',$this->search_tenderer,true);
		$criteria->compare('tendername',$this->tendername,true);
		$criteria->compare('rid',$this->rid,true);
		$criteria->compare('mode',$this->mode);
		$criteria->compare('saletime',$this->saletime,true);
		$criteria->compare('endtime',$this->saletime,true);
		$criteria->compare('opentime',$this->opentime,true);
		$criteria->compare('department',$this->department);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
				'attributes'=>array(
					'search_project'=>array(
						'asc'=>'project.projectname',
						'desc'=>'project.projectname DESC',
					),
					'search_tenderer'=>array(
						'asc'=>'tenderer.name',
						'desc'=>'tenderer.name DESC',
					),
					'*',
				),
			),
		));
	}
	
	/*get the latest tid*/
	public function getTenderId()
	{
		$year = date('Y');
		$sql = "SELECT tid FROM tender where tid LIKE '".$year."%' ORDER BY tid DESC limit 1";
		$query = Yii::app()->db->createCommand($sql);
		$result = $query->queryScalar();
		if($result){
			$num = intval(substr($result,5)) + 1;
			return $year.'_'.str_pad($num,4,'0',STR_PAD_LEFT);
		}else
			return $year.'_0001';
	}
	
	public function checkTid($id)
	{
		$sql = "SELECT tid FROM tender where tid = '".$id."'";
		$query = Yii::app()->db->createCommand($sql);
		$result = $query->queryScalar();
		if($result)
			return false;
		else
			return true;
	}
	
	public function getTenderer($tname)
	{
		$t = Tenderer::model()->findByAttributes(array('name'=>$tname));
		if($t){
			$tendererid = $t->tendererid;
		}else{
			$tenderer = new Tenderer;
			$tenderer->name = $tname;
			$tenderer->save();
			$tendererid = $tenderer->tendererid;
		}
		return $tendererid;
	}
	
	public function getProject($pname,$tendererid)
	{
		$p = Project::model()->findByAttributes(array('projectname'=>$pname));
		if($p){
			$pid = $p->pid;
		}else{
			$newproject = new Project;
			$newproject->projectname = $pname;
			$newproject->organization = $tendererid;
			$newproject->save();
			$pid = $newproject->pid;
		}
		return $pid;
	}
	
	public function getTendername($originalName)
	{
		$re = array();
		$temp = explode("（重招",$originalName);
		$t = Tender::model()->findByAttributes(array('tendername'=>$originalName,'pid'=>$pid));
		if($t){
			$re[0] = $originalName;
			$re[1] = $t->tid;
		}else{
			$re[0] = $originalName;
			$re[1] = '';
		}
		return $re;
	}
	
	public static function itemAlias($type,$code=NULL) {
		$_items = array(
			'ModeStatus' => array(
				'1'=>'场外邀请',
				'2'=>'场外公开',
				'3'=>'进场邀请',
				'4'=>'进场公开',
			),
		);
		if (isset($code))
			return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
		else
			return isset($_items[$type]) ? $_items[$type] : false;
	}
}